import java.util.Random;
import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

/**
 * This class houses all logic used by the marketplace, such as price calculation.
 * @author Stanley Ayzenberg, Michelle Partogi, Adam Perez, Araya Zaesim
 * @version 1.0
 */

public class Marketplace extends JPanel{

	private JButton[] btnBuyAll;
	private JButton[] btnSellAll;
	private JButton[] btnSellOne;
	private JButton[] btnBuyOne;
	
	private JLabel[] lblSellPrice;
	private JLabel[] lblBuyPrice;
	
	private Player player;
	private SolarSystem currentSystem;
	
	private JLabel[] goodNames;
	private int[] buyPrices;
	private int[] sellPrices;
	private SpaceTraderGui gui;
	private String event;

	public String eventCreate(){
		String event;
		Random rand = new Random();
		if (rand.nextInt(100)<10){
			int index = rand.nextInt(7);
			String[] array = {"Lack of Workers","Boredom","Plague","War","Cropfail","Cold","Drought"};
			event = array[index];
		}
		else{
			event = "None";
		}
		this.event = event;
		return event;
	}
	
	public Marketplace(Player player, SolarSystem system, SpaceTraderGui gui) {
		this.gui = gui;
		this.player = player;
		
		setLayout(new GridLayout(11, 7));
		setPreferredSize(new Dimension(700,600));
		
		this.currentSystem = system; //++
		MarketPlaceLogic mpl = new MarketPlaceLogic (system, eventCreate()); //++
		this.buyPrices = mpl.buyPrices();
		this.sellPrices = mpl.sellPrices();
		
		//int[] tempGoods = player.getShip().getGoods();
		int[] tempGoods2 = currentSystem.getGoods();
		
		String[] goods = {"Water" , "Furs","Food","Ore","Games","Firearms","Medicine","Machines","Narcotics","Robots"};
		btnBuyAll = new JButton[10];
		lblSellPrice = new JLabel[10];
		lblBuyPrice = new JLabel[10];
		goodNames = new JLabel[10];
		btnSellAll = new JButton[10];
		btnBuyOne = new JButton[10];
		btnSellOne = new JButton[10];
		for (int i = 0; i<10; i++){
			//creates all the Goods Selling Price Labels
			lblSellPrice[i] = new JLabel("   $"+sellPrices[i]);
			
			//creates all the Goods Selling Price Labels
			lblBuyPrice[i] = new JLabel("   $"+buyPrices[i]);
			
			//creates all the Goods Names label
			System.out.println(goods[i]);
			goodNames[i] = new JLabel(goods[i]);
			
			//creates all the "Buy All" buttons
			System.out.println("hi");
			btnBuyAll[i] = new JButton("Max");
			btnBuyAll[i].addActionListener(new BuyAll(i));
			
			//creates all the "Sell All" buttons
			btnSellAll[i] = new JButton("All");
			btnSellAll[i].addActionListener(new SellAll(i));
			
			//creates all the "Sell One" buttons
			btnSellOne[i] = new JButton("0"); //how much you have in cargo
			btnSellOne[i].addActionListener(new SellOne(i));
			
			//creates all the "Buy One" buttons
			btnBuyOne[i] = new JButton(""+tempGoods2[i]); //how much solar system has
			btnBuyOne[i].addActionListener(new BuyOne(i));
		}

		String[] title = {"Goods", "Sell Price", "Sell", "Sell All", "Buy Price", "Buy", "Buy All"};
		JLabel[] lblTitle = new JLabel[title.length];
		for (int i = 0; i<title.length; i++){
			lblTitle[i] = new JLabel(title[i]);
			add(lblTitle[i]);
			lblTitle[i].setHorizontalAlignment(JLabel.CENTER);
		}
		for (int i = 0; i<10; i++){
			add(goodNames[i]);
			
			add(lblSellPrice[i]);
			lblSellPrice[i].setHorizontalAlignment(JLabel.RIGHT);
			add(btnSellOne[i]);
			add(btnSellAll[i]);
			
			
			add(lblBuyPrice[i]);
			lblBuyPrice[i].setHorizontalAlignment(JLabel.RIGHT);
			add(btnBuyOne[i]);
			add(btnBuyAll[i]);
			
		}
		
	
	}
	
	public void newMarketPlace(SolarSystem system){
		this.currentSystem = system;
		MarketPlaceLogic mpl = new MarketPlaceLogic (system, eventCreate());
		this.buyPrices = mpl.buyPrices();
		this.sellPrices = mpl.sellPrices();
		
		int[] tempGoods = player.getShip().getGoods();
		int[] tempGoods2 = currentSystem.getGoods();

		for (int i = 0; i<10; i++){
			//creates all the Goods Selling Price Labels
			lblSellPrice[i].setText("   $"+sellPrices[i]);
			
			//creates all the Goods Selling Price Labels
			lblBuyPrice[i].setText("   $"+buyPrices[i]);
			
			//creates all the "Sell One" buttons
			btnSellOne[i].setText(""+tempGoods[i]); //how much you have in cargo
			
			//creates all the "Buy One" buttons
			btnBuyOne[i].setText(""+tempGoods2[i]); //how much solar system has
		}	
	}
	
	public void updateMarketPlace(Player player) {
		gui.setCredits(player.getCredits());
		//make this call a gui function
		int [] currentGoods = player.getShip().getGoods();
		
		for(int i=0;i<10;i++){
			btnSellOne[i].setText("" + currentGoods[i]);
		}
		
		if (currentSystem != null) {
			int [] systemGoods = currentSystem.getGoods();
			for(int i = 0; i<10;i++){
				btnBuyOne[i].setText("" + systemGoods[i]);
			}
		}
		
	}
	private class SellOne implements ActionListener{
		private int index;
	
		public SellOne(int index){
			this.index = index;
		
		}
		public void actionPerformed(ActionEvent e) {
			int[] tempGoods = player.getShip().getGoods();
			int[] tempGoods2 = currentSystem.getGoods();
			if (tempGoods[2] > 0 && Goods.FOOD.getMTLU() <= currentSystem.getTechLevel() ) {
				tempGoods[2] -= 1;
				tempGoods2[2] += 1;
				currentSystem.setGoods(tempGoods2);
				player.getShip().setGoods(tempGoods);
				player.getShip().setCargo(player.getShip().getCargo() -1);
				player.setCredits(player.getCredits() + sellPrices[index]);
				updateMarketPlace(player);
			}
		}
	}
	
	private class BuyAll implements ActionListener{
		private int index;
		public BuyAll(int index){
			this.index = index;
		}
		public void actionPerformed(ActionEvent e) {
			int[] tempGoods = player.getShip().getGoods();
			int[] tempGoods2 = currentSystem.getGoods();
			while (player.getShip().isFull()!= true && player.getCredits() >= buyPrices[index] && tempGoods2[index] > 0) { //ADD PRICE for credits
				tempGoods[index] += 1;
				tempGoods2[index] -= 1;
				player.getShip().setGoods(tempGoods);
				player.getShip().setCargo(player.getShip().getCargo() +1);
				currentSystem.setGoods(tempGoods2);
				player.setCredits(player.getCredits() - buyPrices[index]);
				updateMarketPlace(player);
			}
		}
	}
	
	private class SellAll implements ActionListener{
		private int index;
		public SellAll(int index){
			this.index = index;
		}
		public void actionPerformed(ActionEvent e) {
			int[] tempGoods = player.getShip().getGoods();
			int[] tempGoods2 = currentSystem.getGoods();
			while (tempGoods[index] > 0 && Goods.WATER.getMTLU() <= currentSystem.getTechLevel() ) {
				tempGoods[index] -= 1;
				tempGoods2[index] += 1;
				currentSystem.setGoods(tempGoods2);
				player.getShip().setGoods(tempGoods);
				player.getShip().setCargo(player.getShip().getCargo() -1);
				player.setCredits(player.getCredits() + sellPrices[index]);
			}
			updateMarketPlace(player);
		}
	}//end sellall
	
	private class BuyOne implements ActionListener{
		private int index;
		public BuyOne(int index){
			this.index = index;
		}
		public void actionPerformed(ActionEvent e) {
			int[] tempGoods = player.getShip().getGoods();
			int[] tempGoods2 = currentSystem.getGoods();
			if (player.getShip().isFull()!= true && player.getCredits() >= buyPrices[index] && tempGoods2[index] > 0) { //ADD PRICE for credits
				tempGoods[index] += 1;
				tempGoods2[index] -= 1;
				player.getShip().setGoods(tempGoods);
				player.getShip().setCargo(player.getShip().getCargo() +1);
				currentSystem.setGoods(tempGoods2);
				player.setCredits(player.getCredits() - buyPrices[index]);
				updateMarketPlace(player);
			}
		}
	}//end BuyOne 
	
	public String getEvent(){
		return event;
	}
}
